package com.gusha.controller;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysClassifyDao;
import com.gusha.dao.SysTicketDao;
import com.gusha.dao.SysTicketTypeDao;
import com.gusha.model.InfoContent;
import com.gusha.model.SysClassify;
import com.gusha.model.SysTicket;
import com.gusha.model.SysTicketType;
import com.gusha.service.SysTicketService;
import com.gusha.utils.Constants;
import com.gusha.utils.DateUtils;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;



@Controller
@RequestMapping("/gx/sysTicket")
public class SysTicketController {
	
@Autowired
private SysTicketDao sysTicketDao;
@Autowired
private SysTicketService sysTicketService;
@Autowired
private SysClassifyDao sysClassifyDao;
@Autowired
private SysTicketTypeDao sysTicketTypeDao;

@RequestMapping("list")
@SysOperateLog(operate = "查看资讯列表")
public String list(Model model, HttpServletRequest request) {

    //请求参数
    String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
    String ticketNo = WebValueUtil.getWebValue(request, "ticketNo");
    String ticketType = WebValueUtil.getWebValue(request, "ticketType");

    //参数封装
    int pageNo = 0;
    if (StringUtils.isNotBlank(pageIndex)) {
        pageNo = Integer.parseInt(pageIndex);
    }
    SysTicket sysTicket = new SysTicket();
    if (StringUtils.isNotBlank(ticketNo)) {
    	sysTicket.setTicketNo(ticketNo);
    }
//    if (StringUtils.isNotBlank(ticketCategory)) {
//    	sysTicket.setTicketCategory(new SysClassify(ticketCategory));
//    }else {
//    	ticketCategory = "0";
//    }
    if (StringUtils.isNotBlank(ticketType)) {
		SysTicketType sysTicketType = sysTicketTypeDao.getOne(new BigInteger(ticketType));
		sysTicket.setTicketType(sysTicketType);
	}else{
		ticketType = "0";
   }
   

    //获取分页数据
    Sort sort = new Sort(Sort.Direction.DESC, "addTime");
    Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
    Page<SysTicket> page = sysTicketService.findAll(pageable, sysTicket);

    List<SysTicketType> listParent = sysTicketTypeDao.findAll();
    
    model.addAttribute("page", page);
    model.addAttribute("listParent", listParent);
    model.addAttribute("ticketType", new BigInteger(ticketType));
    model.addAttribute("ticketNo", ticketNo);
    return "/admin/sysTicket/list";
}

// 进入添加页面
@RequestMapping("toAdd")
@SysOperateLog(operate = "进入票务添加页面")
public String toAdd(Model model) {
List<SysTicketType> listParent = sysTicketTypeDao.findAll();
  model.addAttribute("listParent", listParent);
    return "/admin/sysTicket/form";
}

@RequestMapping("save")
@ResponseBody
@SysOperateLog(operate = "保存资讯信息")
public JsonResult save(SysTicket sysTicket,HttpServletRequest request) {
    JsonResult result = new JsonResult(false,"保存失败");
    String mummum = WebValueUtil.getWebValue(request, "mummum");
    int mun=Integer.parseInt(mummum);
    if(mun>0) {
    	for(int i=1;i<=mun;i++) {
    		
    		//按照时间生成票的编号
    		DateUtils date=new DateUtils();
    		String str="GS"+date.getDateTime1()+"00"+i;
    		sysTicket.setTicketNo(str);
    		System.out.println(str);
    		sysTicketDao.saveAndFlush(sysTicket);
    	}
    }
    result.setFlag(true);
    result.setMsg("保存成功");
    return result;
}

@RequestMapping("toEdit")
@SysOperateLog(operate = "进入票务编辑页面")
public String toEdit(Model model, HttpServletRequest request) {

    //请求参数
    String id = WebValueUtil.getWebValue(request, "id");
    if (StringUtils.isNotBlank(id)) {
        if(sysTicketDao.existsById(new BigInteger(id))) {
            SysTicket sysTicket = sysTicketDao.getOne(new BigInteger(id));
            model.addAttribute("obj", sysTicket);
        }
    }  
    List<SysClassify> listParent = sysClassifyDao.findAllByParentIdAndIsDelete(new BigInteger(Constants.TICKET),0);
    model.addAttribute("listParent", listParent);
    return "/admin/sysTicket/form";
}
//进入详情
@RequestMapping("selectAll")
@SysOperateLog(operate = "进入票务详情页面")
public String selectAll(Model model, HttpServletRequest request) {

    //请求参数
    String id = WebValueUtil.getWebValue(request, "id");
    if (StringUtils.isNotBlank(id)) {
        if(sysTicketDao.existsById(new BigInteger(id))) {
            SysTicket sysTicket = sysTicketDao.getOne(new BigInteger(id));
            model.addAttribute("obj", sysTicket);
        }
    }  
    List<SysClassify> listParent = sysClassifyDao.findAllByParentIdAndIsDelete(new BigInteger(Constants.TICKET),0);
    model.addAttribute("listParent", listParent);
    return "/admin/sysTicket/check";
}
@RequestMapping("delete")
@ResponseBody
@SysOperateLog(operate = "删除资讯信息")
public JsonResult delete(BigInteger id) {
    JsonResult result = new JsonResult(false,"删除失败");
    if (sysTicketDao.existsById(id)) {
    	sysTicketDao.deleteById(id);
        result.setFlag(true);
        result.setMsg("删除成功");
    }else{
        result.setFlag(false);
        result.setMsg("该对象信息不存在");
        return result;
    }
    return result;
}
}
